package com.ch.clinking.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ch.clinking.R;
import com.ch.clinking.Utils.DownloadImage;
import com.ch.clinking.Utils.NetUtils.OkHttpUtil;
import com.ch.clinking.activity.EditMerStockActivity;
import com.ch.clinking.bean.SkuInfo;

import java.util.HashMap;
import java.util.List;

public class MerEditAdapter extends RecyclerView.Adapter<MerEditAdapter.MyViewHolder> {

    public List<SkuInfo> skuInfoList;
    private LayoutInflater layoutInflater;
    private Context context;
    private Integer operation = 0;
    public Integer batch = 0;
    public Integer changeOperateNum = 1;

    public MerEditAdapter(Context context, List<SkuInfo> skuInfoList) {
        this.skuInfoList = skuInfoList;
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }


    @NonNull
    @Override

    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.list_sku_layout, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public int getItemCount() {
        return skuInfoList.size();
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, @SuppressLint("RecyclerView") int position) {

        SkuInfo skuInfo = skuInfoList.get(position);

//        // 设置默认占位图片
//        Glide.with(context)
//                .load(R.drawable.appicon)
//                .into(holder.image);
//
//        // 加载图片
//        Glide.with(context)
//                .load(OkHttpUtil.getInstance().getIMG_URL() + skuInfo.getImage())
//                .placeholder(R.drawable.appicon)
//                .diskCacheStrategy(DiskCacheStrategy.ALL)  // 缓存策略：缓存原始图片和转换后的图片
//                .thumbnail(0.5f)  // 缩略图大小
//                .error(R.drawable.appicon)
//                .into(holder.image);


        // 执行异步任务加载图片
//        new DownloadImage(holder.image).execute(OkHttpUtil.getInstance().getIMG_URL()+skuInfo.getImage());
        holder.title.setText(skuInfo.getTitle());
        holder.stock.setText(Integer.toString(skuInfo.getStock()));
        holder.finalStock.setText(Integer.toString(skuInfo.getFinalNum()));
        if (skuInfo.getOperation() == 1) {
            holder.addButton.setChecked(true);
        } else {
            holder.reduceButton.setChecked(true);
        }


        if (skuInfo.getOperateNum() == 0) {
//            holder.addOrReduce.setText("请输入");
        } else {
            holder.addOrReduce.setText(Integer.toString(skuInfo.getOperateNum()));
        }


        holder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) group.findViewById(checkedId);


                // 获取选中的单选按钮的文本内容
                String selectedOption = radioButton.getText().toString();
                if (selectedOption.equals("增")) {
                    skuInfo.setOperation(1);
//                    skuInfo.setFinalNum(skuInfo.getOperateNum()*2+skuInfo.getFinalNum());
//                    notifyItemChanged(position);

                } else if (selectedOption.equals("减")) {
                    skuInfo.setOperation(-1);
//                    skuInfo.setFinalNum(Math.max(skuInfo.getFinalNum()- skuInfo.getOperateNum()*2,0));
//                    notifyItemChanged(position);
                }

                if (batch == 0) {
                    String c = holder.addOrReduce.getText().toString();
                    if (c.equals("")) {
                        c = "0";
                    }
                    int change = Integer.parseInt(c);

                    if (skuInfo.getOperation() == 1) {
                        skuInfo.setFinalNum(change + skuInfo.getStock());
                        holder.finalStock.setText(Integer.toString(skuInfo.getFinalNum()));

                    } else if (skuInfo.getOperation() == -1) {
                        skuInfo.setFinalNum(skuInfo.getStock() - change);
                        holder.finalStock.setText(Integer.toString(Math.max(skuInfo.getFinalNum(), 0)));
                    }
                }
            }
        });

        holder.addOrReduce.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (batch == 0) {
                    // Get the text from the first EditText
                    String c = editable.toString();
                    if (c.equals("")) {
                        c = "0";
                    }
                    int change = Integer.parseInt(c);

                    skuInfo.setOperateNum(change);

                    // Update the text of the second EditText
                    if (skuInfo.getOperation() == 1) {
                        skuInfo.setFinalNum(change + skuInfo.getStock());
                        holder.finalStock.setText(Integer.toString(skuInfo.getFinalNum()));

                    } else if (skuInfo.getOperation() == -1) {
                        skuInfo.setFinalNum(skuInfo.getStock() - change);
                        holder.finalStock.setText(Integer.toString(Math.max(skuInfo.getFinalNum(), 0)));
                    }
                }
            }
        });

        holder.finalStock.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String c = editable.toString();
                if (c.equals("")) {
                    c = "0";
                }
                int change = Integer.parseInt(c);
                skuInfo.setFinalNum(Integer.valueOf(String.valueOf(change)));

//                int op = change-skuInfo.getStock();
//                changeOperateNum = 0;

//                if (op < 0) {
//                    holder.reduceButton.setChecked(true);
//                    skuInfo.setOperateNum(Math.abs(op));
//                    skuInfo.setOperation(-1);
//                    holder.addOrReduce.setText(String.valueOf(Math.abs(op)));
//                }else {
//                    holder.addButton.setChecked(true);
//                    skuInfo.setOperateNum(op);
//                    skuInfo.setOperation(1);
//                    holder.addOrReduce.setText(String.valueOf(op));
//                }
//                changeOperateNum = 1;

            }
        });

    }

    public void getView() {
    }

    @SuppressLint("NotifyDataSetChanged")
    public void flash() {
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

//        ImageView image;
        TextView title;
        TextView stock;
        EditText addOrReduce;
        EditText finalStock;
        RadioGroup radioGroup;
        RadioButton addButton;
        RadioButton reduceButton;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

//            this.image = itemView.findViewById(R.id.sku_img);
            this.title = itemView.findViewById(R.id.sku_title);
            this.stock = itemView.findViewById(R.id.sku_stock);
            this.addOrReduce = itemView.findViewById(R.id.sku_operate_num);
            this.finalStock = itemView.findViewById(R.id.sku_final_num);
            this.radioGroup = itemView.findViewById(R.id.sku_add_reduce_button_group);
            this.addButton = itemView.findViewById(R.id.sku_add_button);
            this.reduceButton = itemView.findViewById(R.id.sku_reduce_button);

        }
    }

}
